# @File    : EIP.py
# @Project : web_learn
import os.path
from threading import Thread

from PyQt5 import uic
from PyQt5.QtCore import pyqtSignal, QDateTime, QDate, QTime
from PyQt5.QtWidgets import QMainWindow, QMessageBox

from common.yamlUtil import YamlUtil
from page.overtime_order import OverTimeOrder
from page.rest_order import RestOrder


class EipUI(QMainWindow):
    # 日志路径
    log_path = os.path.abspath(os.path.dirname
                               (os.path.dirname(__file__))) + "\\" + "logs" + "\\" + "run_log"

    """定义槽信号"""
    # 错误提示框
    showWarningSignal = pyqtSignal(str, str)
    # 信息提示框
    showInformationSignal = pyqtSignal(str, str)

    # 未定位到目标元素

    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi("view/EIP.ui")

        self.overtime = OverTimeOrder()
        self.rest_time = RestOrder()

        # 按钮绑定点击事件
        # self.ui.confirm.clicked.connect(lambda: self.overtime.put_order())
        self.ui.confirm.clicked.connect(self.get_input)

        # 绑定槽函数
        self.showWarningSignal.connect(self.show_warning)
        self.showInformationSignal.connect(self.show_information)

        # 数据初始化
        # 设置日期格式
        """加班单"""
        self.ui.start_time.setDisplayFormat("yyyy-MM-dd HH:mm")
        self.ui.end_time.setDisplayFormat("yyyy-MM-dd HH:mm")
        """调休单"""
        self.ui.overtime_start_time.setDisplayFormat("yyyy-MM-dd HH:mm")
        self.ui.overtime_end_time.setDisplayFormat("yyyy-MM-dd HH:mm")
        self.ui.rest_start_time.setDisplayFormat("yyyy-MM-dd HH:mm")
        self.ui.rest_end_time.setDisplayFormat("yyyy-MM-dd HH:mm")
        # 设置默认时间(设置打开时的日期为当前日期，开始时间默认18:15，结束时间默认22:15)
        date = QDate.currentDate()
        start_time = QTime(18, 15)
        rest_start_time = QTime(8, 00)
        start_datetime = QDateTime(date, start_time)
        rest_start_datetime = QDateTime(date, rest_start_time)
        """加班单开始时间"""
        self.ui.start_time.setDateTime(start_datetime)
        """调休单加班开始时间"""
        self.ui.overtime_start_time.setDateTime(start_datetime)
        """调休上班时间"""
        self.ui.rest_start_time.setDateTime(rest_start_datetime)
        end_time = QTime(22, 15)
        rest_end_time = QTime(17, 15)
        end_datetime = QDateTime(date, end_time)
        rest_end_datetime = QDateTime(date, rest_end_time)
        """加班单结束时间"""
        self.ui.end_time.setDateTime(end_datetime)
        """调休单加班结束时间"""
        self.ui.overtime_end_time.setDateTime(end_datetime)
        """调休下班时间"""
        self.ui.rest_end_time.setDateTime(rest_end_datetime)

        # 数据存储
        self.da = YamlUtil("data.yaml")
        self.conf = YamlUtil("config.yaml")

        # 连接槽信号，tab改变时触发
        # self.ui.tabWidget.currentChanged.connect(self.on_tab_changed)

    def open_web(self):
        pass

    # 获取输入数据
    def get_input(self):
        def check():
            data = {}
            work_number = self.ui.work_number.text()
            password = self.ui.password.text()

            # if work_number == "":
            #     self.showWarningSignal.emit('错误', '请输入工号!!!')
            # if password == "":
            #     self.showWarningSignal.emit('错误', '请输入密码!!!')

            overtime_category = self.ui.overtime_category.currentText()
            overtime_cause = self.ui.overtime_cause.text()
            overtime_type = self.ui.overtime_type.currentText()
            start_time = self.ui.start_time.text()
            end_time = self.ui.end_time.text()
            overtime_start_time = self.ui.overtime_start_time.text()
            overtime_end_time = self.ui.overtime_end_time.text()
            rest_start_time = self.ui.rest_start_time.text()
            rest_end_time = self.ui.rest_end_time.text()
            data.update({
                'work_number': work_number,
                'password': password,
                'overtime_category': overtime_category,
                'overtime_cause': overtime_cause,
                'overtime_type': overtime_type,
                'start_time': start_time,
                'end_time': end_time,
                'overtime_start_time': overtime_start_time,
                'overtime_end_time': overtime_end_time,
                'rest_start_time': rest_start_time,
                'rest_end_time': rest_end_time
            })
            self.da.clean_yaml()
            re = self.conf.yaml_template(data)
            self.da.write_yaml(re)
            # 存储数据后打开浏览器
            """判断填加班单还是调休单"""
            if self.ui.tabWidget.currentIndex() == 0:
                self.overtime.put_order()
            elif self.ui.tabWidget.currentIndex() == 1:
                self.rest_time.half_of_put_order()

        check_thread = Thread(target=check)
        check_thread.start()

    # 弹出提示框的方法
    def show_warning(self, title, text_warning):
        QMessageBox.warning(self.ui, title, text_warning)

    # 弹出确认窗口
    def show_information(self, title, text_info):
        QMessageBox.information(self.ui, title, text_info)

    # 获取当前tab的索引值
    # def on_tab_changed(self, index):
    #     print(f"当前选中的Tab索引是：{index}")
    #     print(type(self.ui.tabWidget.currentIndex()))
